package com.example.leetdemo.finash;


import java.util.ArrayList;
import java.util.List;

public class Solution199 {

    public class TreeNode {

        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public List<Integer> rightSideView(TreeNode root) {
        List<TreeNode> list = new ArrayList<>();
        List<Integer> list1 = new ArrayList<>();
        if (root != null) {
            list.add(root);
        }
        while (list.size() > 0) {
            list1.add(list.get(list.size() - 1).val);
            List<TreeNode> temp = new ArrayList<>();
            for (TreeNode treeNode : list) {
                if (treeNode.left != null) {
                    temp.add(treeNode.left);
                }
                if (treeNode.right != null) {
                    temp.add(treeNode.right);
                }
            }
            list = temp;
        }
        return list1;
    }
}
